package com.study4j.common.component.impl;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.stereotype.Component;

import com.study4j.common.component.ICache;
import com.study4j.common.exception.BaseException;
import com.study4j.common.exception.ServerException;
import com.study4j.common.utils.StringUtil;

/**
 * @description
 * @author Jiangbin
 * @date 2019年10月7日 下午9:43:31
 */
@Component
public class RedisCacheImpl implements ICache {

	@Autowired
	private RedisOperations<String, String> stringRedisTemplate;

	@Override
	public void put(String key, String value) {
		this.stringRedisTemplate.opsForValue().set(key, value);
	}

	@Override
	public void put(String key, String value, long expire) {
		this.stringRedisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
	}

	@Override
	public String get(String key) {
		return StringUtil.isEmpty(key) ? "" : (String) this.stringRedisTemplate.opsForValue().get(key);
	}

	@Override
	public Long increment(String key, long delta) {
		return this.stringRedisTemplate.opsForValue().increment(key, delta);
	}

	@Override
	public boolean exists(String key) {
		return StringUtil.isEmpty(key) ? false : this.stringRedisTemplate.hasKey(key).booleanValue();
	}

	@Override
	public boolean expire(String key, Long seconds) {
		return StringUtil.isEmpty(key) ? false
				: this.stringRedisTemplate.expire(key, seconds.longValue(), TimeUnit.SECONDS).booleanValue();
	}

	@Override
	public void delKeyByPrefix(String prefix) {
		if (StringUtil.isEmpty(prefix)) {
			throw new BaseException("Del key by prefix, but prefix is null.");
		} else {
			Set keys = this.stringRedisTemplate.keys(prefix + "*");
			this.stringRedisTemplate.delete(keys);
		}
	}

	@Override
	public void delKeyBySuffix(String suffix) {
		if (StringUtil.isEmpty(suffix)) {
			throw new ServerException("del key by suffix ,but suffix is null.");
		} else {
			Set keys = this.stringRedisTemplate.keys("*" + suffix);
			this.stringRedisTemplate.delete(keys);
		}
	}

	@Override
	public void hput(String key, String hashKey, Object value) {
		HashOperations stringObjectObjectHashOperations = this.stringRedisTemplate.opsForHash();
		stringObjectObjectHashOperations.put(key, hashKey, value.toString());
	}

	@Override
	public String hget(String key, String hashKey) {
		HashOperations stringObjectObjectHashOperations = this.stringRedisTemplate.opsForHash();
		String valObj = (String) stringObjectObjectHashOperations.get(key, hashKey);
		return valObj != null ? valObj : null;
	}

	@Override
	public Long hdel(String key, String hashKey) {
		return this.stringRedisTemplate.opsForHash().delete(key, new Object[] { hashKey });
	}

	@Override
	public void delKey(String key) {
		this.stringRedisTemplate.delete(key);
	}

}
